package net.appositedesigns.appshare;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class AppListAdapter extends BaseAdapter implements Filterable {

	
	public static class ViewHolder 
	{
	  public TextView appName;
	  public ImageView appIcon;
	}
	  
	private Context mContext;
	private List<AppListEntry> apps;
	private LayoutInflater mInflater;
	 
	public AppListAdapter(Context context, List<AppListEntry> apps) {
		super();
		mContext = context;
		this.apps = apps;
		mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if(apps == null)
		{
			return 0;
		}
		else
		{
			return apps.size();
		}
	}

	@Override
	public Object getItem(int arg0) {

		if(apps == null)
			return null;
		else
			return apps.get(arg0);
	}

	@Override
	public long getItemId(int position) {

		return position;
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		  ViewHolder holder = null;
          if (convertView == null) {
              convertView = mInflater.inflate(R.layout.applist_item, parent, false);
              holder = new ViewHolder();
              holder.appName = (TextView)convertView.findViewById(R.id.appList_appName);
              holder.appIcon = (ImageView)convertView.findViewById(R.id.appList_appIcon);
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder)convertView.getTag();
          }
          holder.appName.setText(apps.get(position).getAppName());
          holder.appIcon.setImageDrawable(apps.get(position).getAppIcon());
          return convertView;
      }

	@Override
	public Filter getFilter() {
		
		return new Filter(){

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				// TODO Auto-generated method stub
				
			}
			
			
		};
		
	}

}